﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Listocky;
using System.ComponentModel;
using System.Windows;

namespace Listecky
{
    class DataSource : List<Listocky.QAModel>, INotifyPropertyChanged
    {
        private bool _dirty;
        private String myName;
        private Random random;
        QAModel currentContent;

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string prop)
        {
            if( PropertyChanged != null )
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        public QAModel CurrentContent
        {
            get { return currentContent; }
            set { currentContent = value; OnPropertyChanged("CurrentContent"); }
        }

        public DataSource()
        {
            SetDirty(false);
            random = new Random(123);
            CurrentContent = new QAModel();
        }

        public void SetDirty( bool dirty )
        {
            _dirty = dirty;
        }

        // sets dirty to only true - for delegate reason
        void SetDirty()
        {
            SetDirty(true);
        }

        // Creates new model, fully capable of doing things
        private QAModel CreateNewModel()
        {
            QAModel m = new QAModel();
            m._contentChanged += new QAModel.ContentChanged(this.SetDirty);
            Add(m);
            return m;
        }

        public void Load( String name )
        {
            myName = name;
            Clear();
            System.IO.StreamReader reader = new System.IO.StreamReader(name);
            while (reader.EndOfStream == false)
            {
                QAModel m = CreateNewModel();
                m.Question = reader.ReadLine();
                m.Answer = reader.ReadLine();
                m.Hint = reader.ReadLine();
            }
            // if the number is 0, it is invalid
            if ( Count != 0 )
            {
                CurrentContent = GetNext();
            }
            // this is not dirty anymore, we just loaded
            SetDirty(false);
        }
        
        public void Save(String name)
        {
            if (name == myName)
                SetDirty(false);
            System.IO.StreamWriter writer = new System.IO.StreamWriter(name);
            foreach (QAModel md in this)
            {
                md.Save(writer);
            }
            writer.Close();
        }

        public void AddEmptyModel()
        {
            QAModel model = CreateNewModel();
            CurrentContent = model;
        }

        public void SetNext()
        {
            CurrentContent = GetNext();
        }

        public QAModel GetNext( )
        {
            if (Count == 0)
                return CurrentContent;
            int index = IndexOf(CurrentContent);
            index++;
            if ( index >= Count )
                index = 0;
            return this[index];
        }

        public QAModel GetRandom()
        {
            QAModel model;
            if (Count == 0)
                model = new QAModel();
            else
            {
                int index = random.Next(Count);
                while (this[index] == CurrentContent)
                    index = random.Next(Count);
                model = this[index];
            }
            return model;
        }
        public void Close()
        {
            if (_dirty)
            {
                if (MessageBox.Show("Save changes?", "DirtyChanges!", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    Save(myName);
                }
            }
        }
        public void SetRandom()
        {
            CurrentContent = GetRandom();
        }
    }
}
